package com.itHsuDS.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode2 hero1=new HeroNode2(1,"宋江","及时雨");
        HeroNode2 hero2=new HeroNode2(2,"卢俊义","王麒麟");
        HeroNode2 hero3=new HeroNode2(3,"吴用","智多星");
        HeroNode2 hero4=new HeroNode2(4,"林冲","豹子头");
        DoubleLinkedList doubleLinkedList=new DoubleLinkedList();
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);
        doubleLinkedList.list();

        HeroNode2 newHeroNode=new HeroNode2(4,"公孙胜","入云龙");
        doubleLinkedList.update(newHeroNode);
        doubleLinkedList.list();

        doubleLinkedList.delete(4);
        doubleLinkedList.list();
    }
}

class DoubleLinkedList{
    private HeroNode2 head=new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }

    public void add(HeroNode2 heroNode){
        //因为head节点不能动，因此需要一个辅助变量，temp
        HeroNode2 temp=head;
        //遍历链表
        while(true){
            //找到链表的最后
            if(temp.next==null){
                break;
            }
            //后移
            temp=temp.next;
        }
        //当退出while循环时，temp 已经指向最后的节点
        temp.next=heroNode;
        heroNode.pre=temp;
    }

    public void update(HeroNode2 newHeroNode){
        if(head.next==null){
            System.out.println("链表为空");
            return;
        }
        //找到需要修改的节点
        HeroNode2 temp=head.next;
        boolean flag=false;//表示是否找到该节点
        while(true){
            if(temp==null) {
                break;//到链表末了
            }
            if(temp.no== newHeroNode.no){
                //找到了
                flag=true;
                break;
            }
            temp=temp.next;
        }
        //根据flag判断是否找到
        if(flag){
            temp.name= newHeroNode.name;
            temp.nickname= newHeroNode.nickname;
        }else {
            //没找到
            System.out.println("没找到编号为"+newHeroNode.no+"的节点");
        }
    }

    public void delete(int no){
        if(head.next==null){
            System.out.println("链表为空，无法删除");
            return;
        }
        HeroNode2 temp=head.next;
        boolean flag=false;//标志是否找到待删除节点
        while(true){
            if(temp==null){//链表遍历完了，还没找到
                break;
            }
            if(temp.no==no){//找到要删除的节点
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if(flag){
            temp.pre.next=temp.next;
            //如果是最后一个节点就不需要执行下面那句话，不然就空指针异常
            if(temp.next!=null){
                temp.next.pre=temp.pre;
            }
        }else {
            System.out.println("没有找到要删除的节点："+no);
        }
    }

    //显示链表
    public void list(){
        //判断链表是否为空
        if(head.next==null){
            return;
        }
        HeroNode2 temp=head.next;
        while(true){
            if(temp==null){
                break;
            }
            System.out.println(temp);
            //后移
            temp=temp.next;
        }
    }
}

class HeroNode2{
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    //为了显示方便，重写toString
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}